﻿(function () {
    $(function () {
        var _hazardSourceService = abp.services.app.hazardSource;
        var _$modal = $("#HazardSourceCreateModal");
        var _$form = _$modal.find("form");

        var _$modaledit = $("#HazardSourceEditModal");
        var _$formedit = _$modaledit.find("form");

        //新增保存
        _$form.find('button[type=submit]').click(function (e) {
            e.preventDefault();

            if (!_$form.valid()) {
                return;
            }
           //拿到RiskPointId和作业步骤

            var RiskPointId = $("#RiskPointId").val();
            var JobSteps = new Array($("#jobstep1").val(), $("#jobstep2").val(), $("#jobstep3").val(), $("#jobstep4").val());
            //var hazardsource = _$form.serializeFormToObject();
            
            abp.ui.setBusy(_$modal);

            _hazardSourceService.insertJobSteps(RiskPointId, JobSteps).done(function () {
                _$modal.modal('hide');
                location.reload(true); //reload page to see edited user!
            }).always(function () {
                abp.ui.clearBusy(_$modal);
            });
        });




        //打开新增窗口，传递参数HazardSourceId
        $('#HazardSourceCreateModal').on('show.bs.modal', function (event) {
            var button = $(event.relatedTarget); // Button that triggered the modal
            var riskpointId = button.data('riskpoint-id'); // Extract info from data-* attributes
            var modal = $(this);
            modal.find('#RiskPointId').val(riskpointId);
        })



        //刷新页面
        $("#RefreshButton").click(function () {
            refreshpost()
        });

        function refreshpost() {
            location.reload();
        }

        //删除

        $(".delete-hazardsource").click(function () {
            var hazardsourceId = $(this).attr("data-hazardsource-id");
            var hazardsourceName = $(this).attr("data-hazardsource-name");
            
            deleteHazardSource(hazardsourceId, hazardsourceName);
        });

        function deleteHazardSource(hazardsourceId, hazardsourceName) {

            abp.message.confirm(
                abp.utils.formatString(abp.localization.localize('AreYouSureWantToDelete', 'DualControl'), hazardsourceName),
                function (isConfirmed) {
                    if (isConfirmed) {
                        _hazardSourceService.deleteHazardSource({
                            id: hazardsourceId
                        }).done(function () {
                            refreshpost();
                        });
                    }
                }
            );
        }

        //编辑风险源
        //$(".edit-hazardsource").click(function (e) {
        //   // e.preventDefault();
        //    var hazardsourceId = $(this).attr("data-hazardsource-id");          

        //    $('#HazardSourceEditModal').modal("show");

        //    //$.ajax({
        //    //    url: abp.appPath + 'HazardSources/EditHazardSourceModal?hazardsourceId=' + hazardsourceId,
        //    //    type: 'POST',
        //    //    contentType: 'application/html',
        //    //    success: function (content) {
        //    //        $('#HazardSourceEditModal div.modal-content').html(content);
        //    //    },
        //    //    error: function (e) { }
        //    //});
        //});

        //打开编辑窗口，传递参数HazardSourceId
        $('#HazardSourceEditModal').on('show.bs.modal', function (event) {
            var button = $(event.relatedTarget); // Button that triggered the modal
            var hazardsourceId = button.data('hazardsource-id'); // Extract info from data-* attributes

            var jobstep = button.data('hazardsource-jobstep');

            var modal = $(this);
            modal.find('#Id').val(hazardsourceId);

            modal.find("#JobStep").val(jobstep);
        });

        //修改保存

       

        _$formedit.find('button[type=submit]').click(function (e) {
            e.preventDefault();

            if (!_$form.valid()) {
                return;
            }
            var hazardsource = _$formedit.serializeFormToObject();

            abp.ui.setBusy(_$modal);

            _hazardSourceService.createOrUpdateHazardSource({ hazardsource}).done(function () {
                _$modal.modal('hide');
                location.reload(true); //reload page to see edited user!
            }).always(function () {
                abp.ui.clearBusy(_$modal);
            });
        });


        //模态框关闭时，清除其中的内容。
        _$modal.on("hide.bs.modal", function () {
            _$form[0].reset();
        });
    });
})();